package com.zsrd.lessons.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @标题: ListUtils
 * @项目名称 辽宁营销集约后端
 * @描述: ListUtils 描述
 * @作者 xupan
 * @日期 2021/10/28 14:12
 */
public class ListUtils {

    /**
     * 利用subList方法进行分页
     *
     * @param list        分页数据
     * @param pagesize    页面大小
     * @param currentPage 当前页面
     */
    public static <T> List<T> pageBySubList(List list, int pagesize, int currentPage) {
        int totalcount = list.size();
        int pagecount = 0;
        List<T> subList;
        int m = totalcount % pagesize;
        if (m > 0) {
            pagecount = totalcount / pagesize + 1;
        } else {
            pagecount = totalcount / pagesize;
        }
        int startNum = (currentPage - 1) * pagesize;
        int endNum = pagesize * currentPage;
        System.out.println("起止位置：" + startNum + ",结束位置：" + endNum + ",当前页："
                + currentPage + ",每页显示条数：" + pagesize + "，总页码：" + pagecount + "，总记录数：" + totalcount + ",M的值：" + m);
        if (m == 0) {
            subList = list.subList(startNum, endNum);
        } else {
            if (currentPage == pagecount) {
                subList = list.subList(startNum, totalcount);
            } else {
                subList = list.subList(startNum, endNum);
            }
        }

        return subList;
    }


    /**
     * 循环截取某页列表进行分页
     *
     * @param dataList    分页数据
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     */

    public static <T> List<T> page(List<T> dataList, int pageSize, int currentPage) {

        List<T> currentPageList = new ArrayList<>();

        if (dataList != null && dataList.size() > 0) {

            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);

            for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {

                T data = dataList.get(currIdx + i);

                currentPageList.add(data);
            }
        }
        return currentPageList;

    }

    public static boolean isEmpty(List l) {
        if (l == null || l.size() == 0) {
            return true;
        }
        return false;
    }


}
